/*********************************************************
 * 
 *********************************************************/ 
package gui;

import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import workoutModel.weightliftingSet;
import database.DB_connection;

/*************************************************************
 * 
 * 
 * @author Brian Renzenbrink
 *
 *************************************************************/
public class previousWorkoutGUI extends JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 3149825197719836766L;
	private int currentCol = 0;
	private Container cPane;
	private JFrame frame;
	//private DB_connection conn;
	private ArrayList<weightliftingSet> workouts;



	/*****************************************************************
	 * A constructor for creating a previous workouts chart jframe
	 *****************************************************************/
	public previousWorkoutGUI(ArrayList<weightliftingSet> workouts){
		//Using the parameters, create a printable JFrame containg
		//all of the previous workout data
		this.workouts = workouts;
		initialize();
	}

	/*****************************************************************
	 * An initialization method for the previous workouts chart
	 *****************************************************************/
	public void initialize(){
		//Create and set up the window.
		frame = new JFrame("Previous Workouts");
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setSize(750, 500);

		//Create a scrollpane named cPane
		cPane = new JPanel();
		frame.getContentPane().add(new JScrollPane(cPane));

		//Add cPane to the contentPane
		buildPane(cPane);

		//Display the window.
		frame.setVisible(true);
	}
		
	/******************************************************************
	 * This method adds the basic components to a given pane.
	 * @param pane: The pane to add the components to.
	 ******************************************************************/
	public void buildPane(Container pane) {
		cPane.setLayout(new GridBagLayout());
		cPane.setSize(750, 500);
		GridBagConstraints c = new GridBagConstraints();


		//The title row
		Font titleFont = new Font(Font.SERIF, Font.BOLD, 14);
		JLabel titleLabel = new JLabel("PREVIOUS EXERCISES");
		titleLabel.setFont(titleFont);
		c.anchor = GridBagConstraints.NORTH;
		c.gridy = 0;
		c.gridx = 2;
		cPane.add(titleLabel, c);

		//The field row - Contains labels for Exercise, Weight Type, Weight, Reps, Date and Location
		Font fieldFont = new Font(Font.SERIF, Font.PLAIN, 12);
		JLabel exerciseLabel = new JLabel("Exercise");
		exerciseLabel.setSize(20, 12);
		//exerciseLabel.setFont(fieldFont);
		c.anchor = GridBagConstraints.NORTH;
		c.weightx = 0.5;
		c.gridx = 0;
		c.gridy = 2;
		cPane.add(exerciseLabel, c);

		JLabel weightTypeLabel = new JLabel("Weight Type");
		//exerciseLabel.setFont(fieldFont);
		c.anchor = GridBagConstraints.NORTH;
		c.weightx = 0.5;
		c.gridx = 1;
		c.gridy = 2;
		cPane.add(weightTypeLabel, c);

		JLabel weightLabel = new JLabel("Weight");
		//exerciseLabel.setFont(fieldFont);
		c.anchor = GridBagConstraints.NORTH;
		c.weightx = 0.5;
		c.gridx = 2;
		c.gridy = 2;
		cPane.add(weightLabel, c);

		JLabel repsLabel = new JLabel("Reps");
		//exerciseLabel.setFont(fieldFont);
		c.anchor = GridBagConstraints.NORTH;
		c.weightx = 0.5;
		c.gridx = 3;
		c.gridy = 2;
		cPane.add(repsLabel, c);

		JLabel notesLabel = new JLabel("           Date           ");
		notesLabel.setSize(20, 12);
		//exerciseLabel.setFont(fieldFont);
		c.anchor = GridBagConstraints.NORTH;
		c.weightx = 0.5;
		c.gridx = 4;
		c.gridy = 2;
		cPane.add(notesLabel, c);
		
		JLabel locLabel = new JLabel("    Location     ");
		notesLabel.setSize(20, 12);
		//exerciseLabel.setFont(fieldFont);
		c.anchor = GridBagConstraints.NORTH;
		c.weightx = 0.5;
		c.gridx = 5;
		c.gridy = 2;
		cPane.add(locLabel, c);

		currentCol = 3;
		
		//The data row - Contains textfields for Exercise, Weight Type, Weight, Reps, and Notes
		//Add the data for the given workout sets
		Iterator<weightliftingSet> iteratorW; 
		iteratorW = workouts.iterator();
		
		while(iteratorW.hasNext()){
			//call the addNewSet() method
			addNewWeightliftingSet((weightliftingSet) iteratorW.next());
			//System.out.println(iteratorW.next().toString());
		}
	}//end buildPane
	
	/***************************************************************
	 * A method for adding new sets to the printout
	 ***************************************************************/
	public void addNewWeightliftingSet(weightliftingSet ws){
		GridBagConstraints c = new GridBagConstraints();
		
		//A single set being output as a row of jlabels
		String exerciseWithOrder;
		exerciseWithOrder = String.valueOf(ws.getOrder()) + " " + ws.getExercise();
		JLabel exerciseLabel = new JLabel(exerciseWithOrder);
		exerciseLabel.setSize(20, 12);
		//exerciseLabel.setFont(fieldFont);
		c.anchor = GridBagConstraints.WEST;
		c.weightx = 0.5;
		c.gridx = 0;
		c.gridy = currentCol;
		cPane.add(exerciseLabel, c);

		JLabel weightTypeLabel = new JLabel(ws.getWeightType());
		//exerciseLabel.setFont(fieldFont);
		c.anchor = GridBagConstraints.NORTH;
		c.weightx = 0.5;
		c.gridx = 1;
		c.gridy = currentCol;
		cPane.add(weightTypeLabel, c);

		;
		JLabel weightLabel = new JLabel(String.valueOf(ws.getWeight()));
		//exerciseLabel.setFont(fieldFont);
		c.anchor = GridBagConstraints.NORTH;
		c.weightx = 0.5;
		c.gridx = 2;
		c.gridy = currentCol;
		cPane.add(weightLabel, c);

		JLabel repsLabel = new JLabel(String.valueOf(ws.getReps()));
		//exerciseLabel.setFont(fieldFont);
		c.anchor = GridBagConstraints.NORTH;
		c.weightx = 0.5;
		c.gridx = 3;
		c.gridy = currentCol;
		cPane.add(repsLabel, c);

		JLabel notesLabel = new JLabel(new java.text.SimpleDateFormat("yyyy/MM/dd").format(ws.getDate()));
		notesLabel.setSize(20, 12);
		//exerciseLabel.setFont(fieldFont);
		c.anchor = GridBagConstraints.NORTH;
		c.weightx = 0.5;
		c.gridx = 4;
		c.gridy = currentCol;
		cPane.add(notesLabel, c);
		
		JLabel locLabel = new JLabel(ws.getLocation());
		notesLabel.setSize(20, 12);
		//exerciseLabel.setFont(fieldFont);
		c.anchor = GridBagConstraints.NORTH;
		c.weightx = 0.5;
		c.gridx = 5;
		c.gridy = currentCol;
		cPane.add(locLabel, c);
		
		currentCol++;
	}

}//end previousWorkoutGUI
